﻿using System;
using BasecampRestAPI;
using NUnit.Framework;
using TestBasecampRestAPI.Fakes;
using TestBasecampRestAPI.TestMoveToDoList;

namespace TestBasecampRestAPI
{
	[TestFixture]
	public class TestPost
	{
		private FakeBaseCamp _camp;
		private FakeRestWebService _service;
		private IPost _post;

		[SetUp]
		public void SetUp()
		{
			_camp = FakeBaseCamp.GetInstance();
			_service = FakeRestWebService.GetInstance();
			_camp.ServiceGetFakeResult = _service;
			_post = Post.GetInstance(_camp, TestBasecamp.GetElementForXml(Properties.Resources.FakePost));
		}

		[Test]
		public void Construct()
		{
			Assert.IsNotNull(_post);
			Assert.AreEqual(4404, _post.Id);
			Assert.AreEqual("Test Post", _post.Title);
			Assert.AreEqual("This is the body.", _post.Body);
			Assert.AreEqual(DateTime.Parse("12-03-1969 16:45:45"), _post.PostedOn);
			Assert.AreEqual(2, _post.CommentsCount);
			Assert.AreEqual(0, _post.AttachmentsCount);
			Assert.IsFalse(_post.UseTextile);
			Assert.AreEqual("This is the extended body.", _post.ExtendedBody);
			Assert.AreEqual("This is the display body.", _post.DisplayBody);
			Assert.AreEqual("This is the display extended body.", _post.DisplayExtendedBody);
			Assert.IsFalse(_post.Private);
		}

		[Test]
		public void GetProject()
		{
			_camp.GetProjectByIdFakeResult =
				Project.GetInstance(_camp, TestBasecamp.GetElementForXml(Properties.Resources.FakeProject));

			IProject project = _post.Project;
			Assert.IsNotNull(project);
			Assert.AreEqual(6969, project.Id);
		}

		[Test]
		public void GetCategory()
		{
			_camp.GetCategoryByIdFakeResult =
				Category.GetInstance(_camp, TestBasecamp.GetElementForXml(Properties.Resources.FakeCategory));

			ICategory category = _post.Category;
			Assert.IsNotNull(category);
			Assert.AreEqual(1366, category.Id);
		}

		[Test]
		public void GetAuthor()
		{
			_camp.GetPersonByIdFakeResult =
				Person.GetInstance(_camp, TestBasecamp.GetElementForXml(Properties.Resources.FakePerson));

			IPerson author = _post.Author;
			Assert.IsNotNull(author);
			Assert.AreEqual(3514656, author.Id);
		}

		[Test]
		public void GetMilestone()
		{
			FakeProject project = new FakeProject { Id = 6969 };
			project.GetMilestoneByIdFakeResult = Milestone.GetInstance(_camp,
				TestBasecamp.GetElementForXml(Properties.Resources.FakeMilestone));
			_camp.GetProjectByIdFakeResult = project;

			IMilestone milestone = _post.Milestone;
			Assert.IsNotNull(milestone);
			Assert.AreEqual(10808780, milestone.Id);
		}

		[Test]
		public void PathForId()
		{
			Assert.AreEqual("posts/1336.xml", Post.PathForId(1336));
		}

		[Test]
		public void GetRecentComments()
		{
			IComment fakeComment1 = new FakeComment { Id = 1414 };
			IComment fakeComment2 = new FakeComment { Id = 1415 };
			IComment[] fakeComments = new IComment[] { fakeComment1, fakeComment2 };
			_camp.CommentsForResourceFakeResult = fakeComments;
			IComment[] comments = _post.RecentComments;
			Assert.IsNotNull(comments);
			Assert.AreEqual(2, comments.Length);
			Assert.AreEqual(1414, comments[0].Id);
			Assert.AreEqual(1415, comments[1].Id);
			Assert.IsTrue(_camp.CommentsForResourceCalled);
		}
	}
}
